﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace ET.Monitor.DAL
{
    public class FlowDAL
    {
        object objJudge = null;
        int intCount = 0;
        string strSql = "", strErrMsg = "";
        DataTable dt = new DataTable();

        InvokeWebService iw = new InvokeWebService();
        string rtdbRelationAddress = "";
        string rtdbRealTimeAddress = "";
        string rtdbNameSpace = "";
        public FlowDAL()
        {
            rtdbRelationAddress = ConfigurationManager.ConnectionStrings["RelationURL"].ConnectionString;
            rtdbRealTimeAddress = ConfigurationManager.ConnectionStrings["RealTimeURL"].ConnectionString;
            rtdbNameSpace = ConfigurationManager.ConnectionStrings["NameSpace"].ConnectionString;
        }

        /// <summary>
        /// //流程模板查询
        /// </summary>
        /// <returns></returns>
        public DataTable GetFlowList()
        {
            strSql = @"select case T_CAT_ID when 1 then '请假' when 2 then '公章' else '其他' end T_CAT_NAME,T_FLOW_TYPE,T_FLOW_NAME,
                        T_FLOW_DESC,T_SORT_ORDER, case T_STATUS when 0 then '不可用' else '正常' end T_STATUS,T_IS_DEL,T_UPDATETIME,
                        T_DATELINE from T_LEIPI_FLOW order by T_SORT_ORDER desc";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });

            return dt;
        }

        #region 流程模板分页
        /// <summary>
        /// 流程模板数量
        /// </summary>
        /// <returns></returns>
        public int GetFlowCount()
        {
            strSql = "select count(*) from T_LEIPI_FLOW";
            intCount = (int)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunRowCount", new object[] { strSql, strErrMsg });
            return intCount;
        }

        /// <summary>
        /// 流程模板分页内容
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public DataTable GetFlowPaging(int count, int pageIndex)
        {
            strSql = @"select top " + count + " T_ID,case T_CAT_ID when 1 then '请假' when 2 then '公章' else '其他' end T_CAT_NAME,case T_FLOW_TYPE when 0 then '固定流程' else '自由流程' end T_FLOW_TYPE,T_FLOW_NAME,T_FLOW_DESC,T_SORT_ORDER, case T_STATUS when 0 then '不可用' else '正常' end T_STATUS,T_IS_DEL,T_UPDATETIME,T_DATELINE from T_LEIPI_FLOW where T_ID not in(select top  " + count * (pageIndex - 1) + " T_ID from T_LEIPI_FLOW order by T_SORT_ORDER desc ) order by T_SORT_ORDER desc ";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }

        public bool delFlow(string id)
        {
            strSql = "delete from T_LEIPI_FLOW where T_ID = '" + id + "'";
            return (bool)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunNonQuery", new object[] { strSql, strErrMsg });
        }
        #endregion


    }
}
